package cn.scy.util;

import cn.hutool.core.bean.BeanUtil;
import cn.scy.base.PageParam;
import cn.scy.base.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 分页工具类
 *
 * @author scy
 */
@Slf4j
public final class PageUtil {

    private PageUtil() {
    }

    /**
     * 转换为查询参数
     */
    public static <T> Page<T> convert2PageQuery(PageParam pageParam) {
        return new Page<>(pageParam.getCurrent(), pageParam.getSize());
    }

    /**
     * 转换为 PageResult 对象
     */
    public static <T, E> PageResult<T> convert2PageResult(Page<E> page, List<E> sourceList, Class<T> targetClazz) {
        return convert2PageResult(page, BeanUtil.copyToList(sourceList, targetClazz));
    }

    /**
     * 转换为 PageResult 对象
     */
    public static <E> PageResult<E> convert2PageResult(Page<?> page, List<E> sourceList) {
        PageResult<E> pageResult = new PageResult<>();
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        pageResult.setTotal(page.getTotal());
        pageResult.setPages(page.getPages());
        pageResult.setList(sourceList);
        return pageResult;
    }

}
